package com.demo.juc.util;


import jdk.nashorn.internal.objects.annotations.Getter;

import java.util.concurrent.CountDownLatch;

/**
 * @Author: jxm
 * @Description: 倒计时（做减法）
 * @Date: 2024-2-5 16:11
 * @Version: 1.0
 */
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t国,被灭");
                countDownLatch.countDown();
            },CountryEnum.forEach_CountryEnum(i).getRetMessage()).start();
        }
        countDownLatch.await();//等待所有线程执行完
        System.out.println(Thread.currentThread().getName()+"\t"+"秦帝国，一统华夏");
    }


    enum CountryEnum{
        ONE(1,"齐"),TWO(2,"楚"),THREE(3,"燕"),
        FOUR(4,"赵"),FIVE(5,"魏"),SIX(6,"韩"),
        NULL_VALUE(116,"");

        private Integer retCode;
        private String retMessage;


        CountryEnum(Integer retCode, String retMessage){
            this.retCode = retCode;
            this.retMessage = retMessage;
        }

        public static CountryEnum forEach_CountryEnum(int index){
            CountryEnum[] myArray = CountryEnum.values();
            for (CountryEnum element : myArray){
                if(index == element.getRetCode()){
                    return element;
                }
            }
            return CountryEnum.NULL_VALUE;
        }

        public Integer getRetCode() {
            return retCode;
        }

        public String getRetMessage() {
            return retMessage;
        }
    }
}
